import 'package:flutter/material.dart';
import 'package:yzb_flutter/global/application.dart';
import 'package:yzb_flutter/pages/home_main_page.dart';
import 'package:yzb_flutter/pages/home_self_page.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool showAppBar = false;
  String pageTitle = '';
  List<BottomNavigationBarItem> bottomItems = [];
  int _currentIndex = 0;
  
  List tabData = [
    {"title":"主页","icon":Icons.home},
    {"title":"我的","icon":Icons.person},
  ];

  List<Widget> _pageList = [HomeMainPage(),HomeSelfPage()];

  @override
  void initState() {
    super.initState();
    for(int i = 0 ; i < tabData.length;i++){
      bottomItems.add(BottomNavigationBarItem(icon: Icon(tabData[i]["icon"]),title: Text(tabData[i]["title"])));
    }

    pageTitle = tabData[0]["title"];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: buildAppBar(),
      body: buildBody(),
      bottomNavigationBar: buildBottomNav(),
    );
  }

  Widget buildAppBar() {
    return showAppBar
        ? AppBar(
            title: Text(pageTitle),
          )
        : null;
  }

  Widget buildBody() {
    return IndexedStack(
      index: _currentIndex,
      children: _pageList,
    );
  }

  Widget buildBottomNav() {
    return BottomNavigationBar(
      currentIndex: _currentIndex,
      items: bottomItems,
      type: BottomNavigationBarType.fixed,
      selectedItemColor: Color(Application.mColors.primaryColor),
      onTap: _changePage,
    );
  }

  void _changePage(int position) {
    setState(() {
      showAppBar = position != 0;
      _currentIndex = position;
      pageTitle = tabData[position]["title"];
    });
  }
}
